<template>
  <div class="admin-main">
    <el-form ref="form" style="margin-top:25px" :model="adminUser" label-width="100px" size="mini">
      <el-form-item label="账号">
        <el-input :disabled="true" v-model="adminUser.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-button type="primary" size="mini" @click="toUpdatePwd">点击修改</el-button>
      </el-form-item>
      <el-form-item label="管理员名称">
        <el-input v-model="adminUser.name" />
      </el-form-item>
      <el-form-item label="管理员头像">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="header"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit" :loading="updateLoad">修改</el-button>
      </el-form-item>
    </el-form>

    <!-- 修改弹窗 -->
    <el-dialog title="修改" :visible.sync="updateDialog">
      <admin-update-pwd :admin-user="adminUser" @closeUpdateDialog="closeUpdateDialog" />
    </el-dialog>

  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import adminApi from '@/api/admin'
import adminUpdatePwd from './admin-update-pwd'
import { getToken } from '@/utils/auth'
export default {
  components: {
    adminUpdatePwd
  },
  data() {
    return {
      updateDialog: false,
      updateLoad:false,
      adminUser: this.$store.getters.userInfo,
      header: {
        Authorization: getToken()
      },
      imageUrl: this.$store.getters.userInfo.headerUrl, // 图片回显路径
      uploadUrl: process.env.VUE_APP_ADMIN_URL //  图片上传路径
    }
  },
  watch: { // 监听器这里的function不能使用箭头函数替代
    'adminUser.header': function(newVal, oldVal) {
      this.imageUrl = newVal
    }
  },
  created() {
  },
  methods: {
    closeUpdateDialog() {
      this.updateDialog = false
    },
    toUpdatePwd() {
      this.updateDialog = true
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data
      this.adminUser.headerUrl = res.data
      adminApi.updateInfo(this.adminUser).then(res => {
          this.$message.success(res.data)
      })
      this.$store.commit('SET_USERINFO', this.adminUser)
    },
    onSubmit() {
      this.updateLoad = true
      adminApi.updateInfo(this.adminUser).then(res => {
        this.$message.success(res.data)
        this.updateLoad = false
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .admin-main{
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
